/**
 IDEA Configurator
 Copyright (C) 2006 Justin Tomich<tomichj at gmail dot com>

 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License as
 published by the Free Software Foundation; either version 2 of the
 License, or (at your option) any later version.

 This program is distributed in the hope that it will be useful, but
 WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

package configurator.model;

import configurator.model.url.UrlFactory;
import configurator.model.url.RelativePathUrlFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;


/**
 * An IDEA module. Top-level object for IDEA module configuration.
 *
 * @author Justin Tomich
 */
public class Module
{
    // attributes
    private String name; // used as filename
    private final int version = 4;
    private boolean relativePathsOutsideModule;
    private final String type = "JAVA_MODULE";


    // child elements
    private NewModuleRootManager rootManager; // holds almost all children


    public Module(String name, File rootDirectory,
                  boolean relativePathsOutsideModule)
    {
        this.name = name;
        this.relativePathsOutsideModule = relativePathsOutsideModule;
        UrlFactory urlFactory = new RelativePathUrlFactory(rootDirectory,
                relativePathsOutsideModule);
        this.rootManager = new NewModuleRootManager(urlFactory);
    }


    public String getName()
    {
        return name;
    }


    public NewModuleRootManager root()
    {
        return rootManager;
    }


    public Document toDom() throws ParserConfigurationException
    {
        // make the dom
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true);
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document dom = builder.newDocument();

        // make the root module element
        Element module = dom.createElement("module");
        module.setAttribute("version", String.valueOf(version));
        module.setAttribute("relativePaths",
                String.valueOf(relativePathsOutsideModule));
        module.setAttribute("type", type);
        dom.appendChild(module);

        // the empty old-skool module root manager
        ModuleRootManager oldRoot = new ModuleRootManager();
        oldRoot.addToDom(dom, module);

        // the NEW root manager
        rootManager.addToDom(dom, module);

        return dom;
    }


    /**
     * ModuleRootManager is replaced by NewModuleRootManager,  so it's just a
     * tiny dumb inner class.
     */
    private class ModuleRootManager implements XmlElem
    {
        public void addToDom(Document dom, Element parent)
        {
            Element e = dom.createElement("component");
            e.setAttribute("name", "ModuleRootManager");
            parent.appendChild(e);
        }
    }


    public String toString()
    {
        return "Module{" +
               "name='" + name + "'" +
               ", version=" + version +
               ", relativePaths=" + relativePathsOutsideModule +
               ", type='" + type + "'" +
               ", rootManager=" + rootManager +
               "}";
    }


}


